package com.gimi.cloud.bbp.app.consumer.service.settlement.impl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.gimi.cloud.bbp.app.consumer.feign.settlement.SettlementFeignService;
import com.gimi.cloud.bbp.app.consumer.service.settlement.SettlementService;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.mall.MallCartDTO;
import com.gimi.cloud.bbp.dto.mall.SettlementDTO;
import com.gimi.cloud.bbp.dto.order.OrderDTO;
import com.gimi.cloud.bbp.dto.order.ShOrderPayinfoDTO;

/**
 * 结算服务
 * @author lilinfeng
 */
@Service
public class SettlementServiceImpl implements SettlementService {
	@Autowired private SettlementFeignService feignService;

	@Override
	public void generatingClearingInformation(String requestData, HttpServletRequest request,
			HttpServletResponse response) {
		SettlementDTO parseObject = JSON.parseObject(requestData,SettlementDTO.class);
        parseObject.setUserId(Integer.valueOf(request.getHeader("userId")));
        if (parseObject.getCartIdList()==null) {
        	throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"购物车数据ID列表不能为空");
		}
		Util.sendResponse(feignService.generatingClearingInformation(parseObject), response);
	}

	@Override
	public void generatingOrder(String requestData, HttpServletRequest request, HttpServletResponse response) {
		SettlementDTO parseObject = JSON.parseObject(requestData,SettlementDTO.class);
        parseObject.setUserId(Integer.valueOf(request.getHeader("userId")));
        if (parseObject.getCartIdList()==null) {
        	throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"购物车数据ID列表不能为空");
		}
        if (StringUtils.isBlank(parseObject.geteId())) {
        	throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"收货地址主键不能为空");
		}
		if (StringUtils.isBlank(parseObject.getOrderSource())) {
        	throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"订单来源不能为空");
		}
		if (StringUtils.isBlank(parseObject.getOrderType())) {
        	throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"订单类型不能为空");
		}
		Util.sendResponse(feignService.generatingOrder(parseObject), response);
	}

	@Override
	public void submissionOfPaymentStatus(String requestData, HttpServletRequest request,
			HttpServletResponse response) {
		ShOrderPayinfoDTO parseObject = JSON.parseObject(requestData,ShOrderPayinfoDTO.class);
		String opPayChannel = parseObject.getOpPayChannel();
		if (StringUtils.isBlank(opPayChannel)) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"支付渠道参数不能为空");
		}
		Util.sendResponse(feignService.submissionOfPaymentStatus(parseObject), response);
	}

	@Override
	public void buyDirect(String requestData, HttpServletRequest request, HttpServletResponse response) {
		MallCartDTO mallCart = JSON.parseObject(requestData,MallCartDTO.class);
		if (mallCart.getSkuId()==null) {
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"单品ID不能为空");
		}
		mallCart.setUserId(Integer.valueOf(request.getHeader("userId")));
		Util.sendResponse(feignService.buyDirect(mallCart), response);
	}

	@Override
	public void getPaymentParameters(String requestData, HttpServletRequest request, HttpServletResponse response) {
		OrderDTO order = JSON.parseObject(requestData,OrderDTO.class);
		Util.sendResponse(feignService.getPaymentParameters(order), response);
	}

	@Override
	public void getOrderPayAmount(String requestData, HttpServletRequest request, HttpServletResponse response) {
		OrderDTO order = JSON.parseObject(requestData,OrderDTO.class);
		Util.sendResponse(feignService.getOrderPayAmount(order), response);
	}

}